<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>OpenMesh: First Steps - Building a cube</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="logo_align.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="rwth_vci_rgb.jpg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">OpenMesh
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('a04336.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">First Steps - Building a cube </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>This small example shows:</p>
<ul>
<li>How to declare your type <code>MyMesh</code>, </li>
<li>How to add vertices and faces to a mesh, </li>
<li>How to write a mesh using the IO functions.</li>
</ul>
<p>For each program the first step is to define your type <code>MyMesh</code>. OpenMesh supports general polygonal meshes (faces are polygons with varying number of vertices) as well as specialized triangle meshes (all faces are triangles). In this example we want to build a cube from six quadrangles, therefore we choose the polygonal mesh.</p>
<p>OpenMesh also supports different mesh kernels, specifying how all the vertices, edges, and faces are stored internally (see also <a class="el" href="a01229.html">Mesh Kernels</a>). However, the storage must provide an array like interface. For the tutorial we use the supplied ArrayKernel. The predefined combinations of TriMesh/PolyMesh and the kernel are contained in <code>OpenMesh/src/OpenMesh/Core/Mesh</code>, we use the <code>PolyMesh_ArrayKernelT</code>.</p>
 <div class="fragment"><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh&gt;</span></div><div class="line"><span class="keyword">typedef</span> <a class="code" href="a02385.html">OpenMesh::PolyMesh_ArrayKernelT&lt;&gt;</a>  MyMesh;</div></div><!-- fragment --></p>
<p>Now since we have declared our type <code>MyMesh</code>, we only have to add 8 vertices and 6 quadrangles to build a cube. Adding a vertex is done using the <code>add_vertex</code> method. It gets a coordinate and returns a handle to the inserted vertex. We store all handles in an array, since we need them for specifying the faces.</p>
<p><div class="fragment"><div class="line">  vhandle[0] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1, -1,  1));</div><div class="line">  vhandle[1] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>( 1, -1,  1));</div><div class="line">  vhandle[2] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>( 1,  1,  1));</div><div class="line">  vhandle[3] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1,  1,  1));</div></div><!-- fragment --></p>
<p><br />
In order to add a face to the mesh, we have to build a vector holding the handles to the face's vertices. This vector is passed to the <code>add_face</code> method. The following block will create a face from the first four vertices:</p>
<p><div class="fragment"><div class="line">  std::vector&lt;MyMesh::VertexHandle&gt;  face_vhandles;</div><div class="line"></div><div class="line">  face_vhandles.clear();</div><div class="line">  face_vhandles.push_back(vhandle[0]);</div><div class="line">  face_vhandles.push_back(vhandle[1]);</div><div class="line">  face_vhandles.push_back(vhandle[2]);</div><div class="line">  face_vhandles.push_back(vhandle[3]);</div><div class="line">  mesh.add_face(face_vhandles);</div></div><!-- fragment --></p>
<p><br />
The orientation of the face is defined by the order in which the vertices are given: If you look at the frontfacing side of the polygon, then the vertices are in counter-clockwise order.</p>
<p>After creating all of the six faces, we want to write the resulting mesh to standard output. OpenMesh provides some basic input/output methods in the namespace <a class="el" href="a01236.html" title="This namespace contains functions for reading and writing polygonal meshes and a list of supported fi...">OpenMesh::IO</a>:</p>
<p><div class="fragment"><div class="line">    <span class="keywordflow">if</span> ( !<a class="code" href="a01236.html#a18286a792ebf896872f5e26182fc5c7b">OpenMesh::IO::write_mesh</a>(mesh, <span class="stringliteral">&quot;output.off&quot;</span>) )</div></div><!-- fragment --></p>
<p><br />
To use the IO facility of OpenMesh make sure that the include <a class="el" href="a00200_source.html">MeshIO.hh</a> is included first.</p>
 <div class="fragment"><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/IO/MeshIO.hh&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh&gt;</span></div></div><!-- fragment --></p>
<p><br />
The complete source looks like this:</p>
<div class="fragment"><div class="line"><span class="comment">/* ========================================================================= *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> *                               OpenMesh                                    *</span></div><div class="line"><span class="comment"> *           Copyright (c) 2001-2015, RWTH-Aachen University                 *</span></div><div class="line"><span class="comment"> *           Department of Computer Graphics and Multimedia                  *</span></div><div class="line"><span class="comment"> *                          All rights reserved.                             *</span></div><div class="line"><span class="comment"> *                            www.openmesh.org                               *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> *---------------------------------------------------------------------------*</span></div><div class="line"><span class="comment"> * This file is part of OpenMesh.                                            *</span></div><div class="line"><span class="comment"> *---------------------------------------------------------------------------*</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * Redistribution and use in source and binary forms, with or without        *</span></div><div class="line"><span class="comment"> * modification, are permitted provided that the following conditions        *</span></div><div class="line"><span class="comment"> * are met:                                                                  *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * 1. Redistributions of source code must retain the above copyright notice, *</span></div><div class="line"><span class="comment"> *    this list of conditions and the following disclaimer.                  *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright      *</span></div><div class="line"><span class="comment"> *    notice, this list of conditions and the following disclaimer in the    *</span></div><div class="line"><span class="comment"> *    documentation and/or other materials provided with the distribution.   *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * 3. Neither the name of the copyright holder nor the names of its          *</span></div><div class="line"><span class="comment"> *    contributors may be used to endorse or promote products derived from   *</span></div><div class="line"><span class="comment"> *    this software without specific prior written permission.               *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *</span></div><div class="line"><span class="comment"> * &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *</span></div><div class="line"><span class="comment"> * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *</span></div><div class="line"><span class="comment"> * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *</span></div><div class="line"><span class="comment"> * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *</span></div><div class="line"><span class="comment"> * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *</span></div><div class="line"><span class="comment"> * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *</span></div><div class="line"><span class="comment"> * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *</span></div><div class="line"><span class="comment"> * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *</span></div><div class="line"><span class="comment"> * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *</span></div><div class="line"><span class="comment"> * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * ========================================================================= */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="comment">// -------------------- OpenMesh</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/IO/MeshIO.hh&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh&gt;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// ----------------------------------------------------------------------------</span></div><div class="line"></div><div class="line"><span class="keyword">typedef</span> <a class="code" href="a02385.html">OpenMesh::PolyMesh_ArrayKernelT&lt;&gt;</a>  MyMesh;</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// ----------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// Build a simple cube and write it to std::cout</span></div><div class="line">  </div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">  MyMesh mesh;</div><div class="line"></div><div class="line">  <span class="comment">// generate vertices</span></div><div class="line"></div><div class="line">  <a class="code" href="a02389.html#a0fc18e13d70825938274e3faf7b55e01">MyMesh::VertexHandle</a> vhandle[8];</div><div class="line"></div><div class="line">  vhandle[0] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1, -1,  1));</div><div class="line">  vhandle[1] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>( 1, -1,  1));</div><div class="line">  vhandle[2] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>( 1,  1,  1));</div><div class="line">  vhandle[3] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1,  1,  1));</div><div class="line">  vhandle[4] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1, -1, -1));</div><div class="line">  vhandle[5] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>( 1, -1, -1));</div><div class="line">  vhandle[6] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>( 1,  1, -1));</div><div class="line">  vhandle[7] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1,  1, -1));</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// generate (quadrilateral) faces</span></div><div class="line"></div><div class="line">  std::vector&lt;MyMesh::VertexHandle&gt;  face_vhandles;</div><div class="line"></div><div class="line">  face_vhandles.clear();</div><div class="line">  face_vhandles.push_back(vhandle[0]);</div><div class="line">  face_vhandles.push_back(vhandle[1]);</div><div class="line">  face_vhandles.push_back(vhandle[2]);</div><div class="line">  face_vhandles.push_back(vhandle[3]);</div><div class="line">  mesh.add_face(face_vhandles);</div><div class="line"> </div><div class="line">  face_vhandles.clear();</div><div class="line">  face_vhandles.push_back(vhandle[7]);</div><div class="line">  face_vhandles.push_back(vhandle[6]);</div><div class="line">  face_vhandles.push_back(vhandle[5]);</div><div class="line">  face_vhandles.push_back(vhandle[4]);</div><div class="line">  mesh.add_face(face_vhandles);</div><div class="line"></div><div class="line">  face_vhandles.clear();</div><div class="line">  face_vhandles.push_back(vhandle[1]);</div><div class="line">  face_vhandles.push_back(vhandle[0]);</div><div class="line">  face_vhandles.push_back(vhandle[4]);</div><div class="line">  face_vhandles.push_back(vhandle[5]);</div><div class="line">  mesh.add_face(face_vhandles);</div><div class="line"></div><div class="line">  face_vhandles.clear();</div><div class="line">  face_vhandles.push_back(vhandle[2]);</div><div class="line">  face_vhandles.push_back(vhandle[1]);</div><div class="line">  face_vhandles.push_back(vhandle[5]);</div><div class="line">  face_vhandles.push_back(vhandle[6]);</div><div class="line">  mesh.add_face(face_vhandles);</div><div class="line"></div><div class="line">  face_vhandles.clear();</div><div class="line">  face_vhandles.push_back(vhandle[3]);</div><div class="line">  face_vhandles.push_back(vhandle[2]);</div><div class="line">  face_vhandles.push_back(vhandle[6]);</div><div class="line">  face_vhandles.push_back(vhandle[7]);</div><div class="line">  mesh.add_face(face_vhandles);</div><div class="line"></div><div class="line">  face_vhandles.clear();</div><div class="line">  face_vhandles.push_back(vhandle[0]);</div><div class="line">  face_vhandles.push_back(vhandle[3]);</div><div class="line">  face_vhandles.push_back(vhandle[7]);</div><div class="line">  face_vhandles.push_back(vhandle[4]);</div><div class="line">  mesh.add_face(face_vhandles);</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// write mesh to output.obj</span></div><div class="line">  <span class="keywordflow">try</span></div><div class="line">  {</div><div class="line">    <span class="keywordflow">if</span> ( !<a class="code" href="a01236.html#a18286a792ebf896872f5e26182fc5c7b">OpenMesh::IO::write_mesh</a>(mesh, <span class="stringliteral">&quot;output.off&quot;</span>) )</div><div class="line">    {</div><div class="line">      std::cerr &lt;&lt; <span class="stringliteral">&quot;Cannot write mesh to file &#39;output.off&#39;&quot;</span> &lt;&lt; std::endl;</div><div class="line">      <span class="keywordflow">return</span> 1;</div><div class="line">    }</div><div class="line">  }</div><div class="line">  <span class="keywordflow">catch</span>( std::exception&amp; x )</div><div class="line">  {</div><div class="line">    std::cerr &lt;&lt; x.what() &lt;&lt; std::endl;</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<hr>
<address>
<small>
<a href="http://www.rwth-graphics.de" style="text-decoration:none;">
</a>
Project <b>OpenMesh</b>, 
&copy;&nbsp; Computer Graphics Group, RWTH Aachen.
Documentation generated using 
<a class="el" href="http://www.doxygen.org/index.html">
<b>doxygen</b>
</a>.
</small>
</address>
</body>
</html>
